<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>线程池状态</title>
</head>
<style type="text/css">
    body {
        font: normal 11px auto Trebuchet MS, Verdana, Arial, Helvetica, sans-serif;
        color: #4f6b72;
        background: #E6EAE9;
    }

    a {
        color: #c75f3e;
    }

    #mytable {
        width: 800px;
        padding: 0;
        margin: 0;
    }

    caption {
        padding: 0 0 5px 0;
        width: 90%;
        font: italic 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
        text-align: right;
    }

    th {
        font: bold 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
        color: #4f6b72;
        border-right: 1px solid #C1DAD7;
        border-bottom: 1px solid #C1DAD7;
        border-top: 1px solid #C1DAD7;
        letter-spacing: 2px;
        text-transform: uppercase;
        text-align: left;
        padding: 6px 6px 6px 12px;
        background: #CAE8EA no-repeat;
    }

    th.nobg {
        border-top: 0;
        border-left: 0;
        border-right: 1px solid #C1DAD7;
        background: none;
    }

    td {
        border-right: 1px solid #C1DAD7;
        border-bottom: 1px solid #C1DAD7;
        background: #fff;
        font-size: 11px;
        padding: 6px 6px 6px 12px;
        color: #4f6b72;
    }


    td.alt {
        background: #F5FAFA;
        color: #797268;
    }

    th.spec {
        border-left: 1px solid #C1DAD7;
        border-top: 0;
        background: #fff no-repeat;
        font: bold 10px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
    }

    th.specalt {
        border-left: 1px solid #C1DAD7;
        border-top: 0;
        background: #f5fafa no-repeat;
        font: bold 10px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
        color: #797268;
    }

    /*---------for IE 5.x bug*/
    html > body td {
        font-size: 11px;
    }

    body, td, th {
        font-family: 宋体, Arial;
        font-size: 12px;
    }

    div {
        float: left;
    }
</style>
<body>
<div>
    <table id="mytable" cellspacing="0">
        <caption></caption>
        <tr>
            <th scope="col">线程池标识</th>
            <th scope="col">线程池名称</th>
            <th scope="col">线程数</th>
            <th scope="col">任务数</th>
            <th scope="col">拒绝策略</th>
            <th scope="col" width="60">操作</th>
        </tr>
    </table>
</div>
<div style="padding-left:10px">
    <table style="width: 300px">
        <tr>
            <td colspan="2" style="align-content: center">修改线程池</td>
        </tr>
        <tr>
            <td>线程池标识</td>
            <td>
                <input type="text" readonly id="set_threadPoolId">
            </td>
        </tr>
        <tr>
            <td>线程池名称</td>
            <td>
                <input type="text" readonly id="set_threadPoolName">
            </td>
        </tr>
        <tr>
            <td>核心线程数</td>
            <td>
                <input type="number" id="set_corePoolSize">
            </td>
        </tr>
        <tr>
            <td>最大线程数</td>
            <td>
                <input type="number" id="set_maximumPoolSize">
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <button type="button" id="set_thread_btn">修改</button>
            </td>
        </tr>
    </table>
</div>
</body>
<!--<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>-->
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
</html>
<script type="text/javascript">
    let threadMap = {};
    let currentThreadId;
    $(document).ready(function () {
        loadThread();

        $("#set_thread_btn").click(function () {
            saveThread();
        });
    });

    function loadThread() {
        $.getJSON("status?action=status", function (result) {
            if (result && result.data) {
                $.each(result.data, function (index, thread) {
                    let threadPoolId = thread.threadPoolId;
                    let tr = "<tr>" +
                        "        <td class='row'>" + threadPoolId + "</td>" +
                        "        <td class='row'>" + thread.threadPoolName + "</td>" +
                        "        <td class='row'>" +
                        "           Pool Size:" + thread.poolSize + "<br/>" +
                        "           active:" + thread.activeCount + "<br/>" +
                        "           core:" + thread.corePoolSize + "<br/>" +
                        "           max:" + thread.maximumPoolSize + "<br/>" +
                        "           largest:" + thread.largestPoolSize +
                        "        </td>" +
                        "        <td class='row'>" +
                        "           Task Count:" + thread.taskCount + "<br/>" +
                        "           completed:" + thread.completedTaskCount +
                        "        </td>" +
                        "        <td class='row'>" + thread.reject + "</td>" +
                        "        <td class='row'>" +
                        "            <input id='" + threadPoolId + "_radio' type='radio' name='thread_radio' />" +
                        "            <label for='" + threadPoolId + "_radio'>设置</label>" +
                        "        </td>" +
                        "    </tr>";
                    $("#mytable").append(tr);

                    threadMap[threadPoolId] = thread;

                    $("#" + threadPoolId + "_radio").click(function () {
                        setThread(threadPoolId);
                    });
                })
            }
        });
    }

    function setThread(threadPoolId) {
        let thread = threadMap[threadPoolId];
        currentThreadId = threadPoolId;
        $("#set_threadPoolId").val(thread.threadPoolId);
        $("#set_threadPoolName").val(thread.threadPoolName);
        $("#set_corePoolSize").val(thread.corePoolSize);
        $("#set_maximumPoolSize").val(thread.maximumPoolSize);
    }

    function saveThread() {
        if(!currentThreadId){
            alert("请选择待修改的线程池");
            return;
        }
        let corePoolSize = $("#set_corePoolSize").val();
        let maximumPoolSize = $("#set_maximumPoolSize").val();
        let core = parseInt(corePoolSize);
        let max = parseInt(maximumPoolSize);
        if(!core || !max){
            alert("请输入合法的线程池数");
            return;
        }
        if(core > max){
            alert("核心线程池数不能大于最大线程数");
            return;
        }
        let url = "status?action=configureThreadPool"
            + "&threadPoolKey=" + currentThreadId
            + "&corePoolSize=" + corePoolSize
            + "&maximumPoolSize=" + maximumPoolSize;
        $.getJSON(url, function (result) {
            if(result == null || !result.success){
                alert("修改失败");
            }else{
                alert("修改成功")
            }

            location.reload();
        });
    }

</script>